﻿@model IEnumerable<Von.Web.Structs.IdentityRoleInfo>

@{
    ViewBag.Title = "角色管理";
}
<h2>系统角色管理<a class="btn btn-link" onclick="NewRole()">新建角色</a></h2>
<table id="roleList" class="table">
    <tr><th>角色名称</th><th>角色分组</th><th>说明</th><th></th></tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.RoleName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.RoleGroup)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Note)
            </td>
            <td>
                <input type='button' class='btn-link' onclick="EditRole(this,'@item.RoleName')" value="编辑" /> |
                @Html.ActionLink("设置权限", "RoleRightManage", "Admin", new { role = item.RoleName }, new { @class = "btn-link" }) |
                <input type='button' class='btn-danger' onclick="DeleteRole(this,'@item.RoleName')" value="删除" />
            </td>
        </tr>
    }
</table>
<!-- 模态框（Modal） -->
<div class="modal fade" id="roleEditor" tabindex="-1" role="dialog" aria-labelledby="ModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header" style="background-color:grey;">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>
                <h4 class="modal-title" id="ModalLabel">角色编辑</h4>
            </div>
            <div class="modal-body">
                <div class="form-group">
                    <label for="RoleName" class="control-label">角色名称</label>
                    <input id="RoleName" class="form-control" />
                </div>
                <div class="form-group">
                    <label for="RoleGroup" class="control-label">角色分组</label>
                    <input id="RoleGroup" class="form-control" />
                </div>
                <div class="form-group">
                    <label for="Note" class="control-label">说明</label>
                    <textarea id="Note" class="form-control"></textarea>
                </div>
            </div>
            <div class="modal-footer">
                <div class="form-group">
                    <input type="hidden" id="flag" />
                    <input value="保存" class="btn btn-default" onclick="SaveRole()" />
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>
</div>

<script type="text/javascript">
    function selected(d) {
        $("[data-role-sel=true]").removeAttr("data-role-sel");
        if (d != null) $(d).parent().parent().attr("data-role-sel", true);
    }
    function roleToHtml(r) {
        return "<tr><td>" + r.RoleName + "</td><td>" + r.RoleGroup + "</td><td>" + r.Note + "</td><td><input type='button' class='btn-link' onclick=\"EditRole(this,'" + r.RoleName +
            "')\" value='编辑' /> | <a class='btn-link' href=\"/Admin/RoleRightManage?role='" + r.RoleName + "'\">设置权限</a> | <input type='button' class='btn-danger' onclick=\"DeleteRole(this,'" + r.RoleName +
            "')\" value='删除' /></td></tr>";
    }
    function EditRole(d,id) {
        selected(d);
        AjaxGetData('@Url.Ajax("GetRole")', { RoleName: id }, function (data) {
            $("#RoleName").val(data.RoleName);
            $("#RoleGroup").val(data.RoleGroup);
            $("#Note").val(data.Note);
            $("#roleEditor").attr("data-mode", "edit");
            $('#roleEditor').modal('show');
        });
    }
    function NewRole() {
        selected();
        $("#RoleName").val("");
        $("#Note").val("");
        $("#roleEditor").attr("data-mode", "new");
        $('#roleEditor').modal('show');
    }
    function SaveRole() {
        var data = { RoleName: $("#RoleName").val(), RoleGroup: $("#RoleGroup").val(), Note: $("#Note").val(), isNew: $("#roleEditor").attr("data-mode") == "new"}
        AjaxGetData('@Url.Ajax("SaveRole")', data, function (result) {
            if ($("#roleEditor").attr("data-mode") == "edit") $("[data-role-sel=true]").replaceWith(roleToHtml(result));
            else $("#roleList tbody").append(roleToHtml(result))
            $('#roleEditor').modal('hide');
        });
    }
    function DeleteRole(d, id) {
        AjaxGetData('@Url.Ajax("DeleteRole")', { RoleName: id }, function (data) { $(d).parent().parent().remove(); });
    }

</script>